/* 首页、专区页脚本
 ** 2020-10-09
 ** by Molly
 */
var mainVm = new Vue({
  el: '.page-container',
  data: {
    url: {
      page_data: '/antacom/data.Webpage/onlinePc',
      goods_products: '/antacom/data.Goods/products',
      agree_check: '/antacom/data.User/isAgree', // 查询是否同意个人信息保护政策
      agree_submit: '/antacom/data.User/updateAgreeTime', // 同意个人信息保护政策
      path_agree:'/cms/statement'
    },
    page: [],
    agree: true,
    id: MALL.Common.getQueryString('id') ? MALL.Common.getQueryString('id') : false,
  },
  mounted: function () {
    var that = this;
    try {
      this.checkAgree();
    } catch (error) {
      console.error(error)
    }
    if (page_id || this.id) {
      $.ajax({
        type: 'POST',
        url: that.url.page_data,
        async: false,
        data: { id: page_id ? page_id : that.id },
        dataType: 'json',
        success: function (res) {
          if (res.status == '1' && res.data.length > 0) {
            let _page = res.data;
            for (var x in _page) {
              if (_page[x].type != 'custom') {
                continue;
              }
              for (var y in _page[x].links) {
                let _link = _page[x].links[y];
                if (_link.url && _link.url.indexOf('-') >= 0 && _link.url.split('-')[1] == '') {
                  _link.urlType = 'coupon';
                  _link.newUrl = _link.url.split('-')[0];
                } else {
                  _link.urlType = 'normal';
                  _link.newUrl = _link.url;
                }
              }
            }
            that.page = _page;
            that.loadGoods();
          }
        },
        error: function (res) {},
      });
    } else {
      MALL.Common.message({ text: '参数错误，请返重试', time: 2000 });
      setTimeout(function () {
        location.href = '/';
      }, 1500);
      return false;
    }
  },
  methods: {
    loadGoods: function () {
      var that = this;
      for (var x in that.page) {
        var _item = that.page[x];
        if (_item.type != 'goods') continue;
        if (!_item.goodsSn || _item.goodsSn == '' || _item.goodsSn.split(',').length == 0) continue;
        $.ajax({
          type: 'POST',
          url: that.url.goods_products,
          async: false,
          data: { goods_id: _item.goodsSn },
          dataType: 'json',
          success: function (res) {
            if (res.status == '1' && res.data.length > 0) {
              var arr = [];
              for (var y in res.data) {
                if (res.data[y]) {
                  var goodsItem = res.data[y];
                  goodsItem.startIdx = 0;
                  goodsItem.hover = false;
                  goodsItem.image = goodsItem.info.image;
                  for (var i = goodsItem.child.length - 1; i >= 0; i--) {
                    try {
                      if (goodsItem.child[i].image.small.indexOf('/goods_empty.png') > 0) {
                        goodsItem.child.splice(i, 1);
                      }
                    } catch (error) {
                      console.error(error);
                    }
                    try {
                      goodsItem.child[i].hover = false;
                    } catch (error) {
                      console.error(error);
                    }
                  }
                  arr.push(goodsItem);
                }
              }
              _item.goodsList = arr;
              that.$set(that.page, x, _item);
            }
          },
          error: function (res) {},
        });
      }
      this.$nextTick(function () {
        areaInit();
      });
    },
    changeAccordion: function (idx, idx2) {
      var _item = this.page[idx];
      _item.current = idx2;
      this.$set(this.page, idx, _item);
    },
    getCoupon: function (code) {
      if (code == '') {
        MALL.Common.message({ text: '优惠券码不能为空', time: 2000 });
        return false;
      }
      MALL.Common.getCouponByCode({ code: code });
    },
    // 商品组操作
    hoverGoodsItem: function (pageIdx, goodsIdx, hover, event) {
      if (pageIdx < 0 || goodsIdx < 0) {
        return false;
      }
      var pageItem = this.page[pageIdx];

      pageItem.goodsList[goodsIdx].hover = hover || false;
      if (!hover) {
        pageItem.goodsList[goodsIdx].image = pageItem.goodsList[goodsIdx].info.image;
      }
      this.$set(this.page, pageIdx, pageItem);

      this.$nextTick(function () {
        if (!hover) {
          var image = $(event.target).find('.J_goods_img').eq(0);
          image.attr('src', image.attr('original'));
        }
      });
    },
    hoverGoodsThumb: function (pageIdx, goodsIdx, thumbIdx, hover, event) {
      if (pageIdx < 0 || goodsIdx < 0 || thumbIdx < 0) {
        return false;
      }
      var pageItem = this.page[pageIdx];
      var goodsItem = pageItem.goodsList[goodsIdx];

      pageItem.goodsList[goodsIdx].child[thumbIdx].hover = hover || false;
      pageItem.goodsList[goodsIdx].image = hover
        ? goodsItem.child[thumbIdx].image.normal
        : goodsItem.info.image;
      this.$set(this.page, pageIdx, pageItem);

      this.$nextTick(function () {
        if (hover) {
          var image = $(event.target).parents('.J_goods_item').find('.J_goods_img').eq(0);
          image.attr('src', $(event.target).find('img').eq(0).attr('data'));
        }
      });
    },
    clickGoodsThumb: function (pageIdx, goodsIdx, type, event) {
      if (pageIdx < 0 || goodsIdx < 0) {
        return false;
      }
      var pageItem = this.page[pageIdx];
      var goodsItem = pageItem.goodsList[goodsIdx];

      switch (type) {
        case 1:
          if (goodsItem.startIdx > 0) {
            goodsItem.startIdx--;
          }
          break;
        case 2:
          if (goodsItem.startIdx + 5 < goodsItem.child.length) {
            goodsItem.startIdx++;
          }
          break;
      }
      this.$set(this.page, pageIdx, pageItem);
      this.$nextTick(function () {
        var list = $(event.target).siblings('.J_thumbs_list').find('li');
        var image = $(event.target).parents('.J_goods_item').find('.J_goods_img').eq(0);

        image.attr('src', list.eq(goodsItem.startIdx).find('img').attr('data'));
        if (list.find('img[original]').length > list.find('img[src]').length) {
          for (var i = 0; i < list.length; i++) {
            var _itpic = list.eq(i).find('img');
            _itpic.attr('src', _itpic.attr('original'));
          }
        }
      });
    },
    checkAgree() {
      var that = this;
      that.agree = true;

      // try {
      //   var isShowAgree = sessionStorage.getItem('anta_agree_show_time');
      //   if (isShowAgree) {
      //     return;
      //   }
      // } catch (error) {}
      $.ajax({
        type: 'POST',
        url: that.url.agree_check,
        async: true,
        data: {},
        dataType: 'json',
        success: function (data) {
          if (data.status == 1 && data.data && !data.data.agree) {
            that.agree = false;
          }
        },
      });
    },
    updateAgree() {
      var that = this;
      $.ajax({
        type: 'POST',
        url: that.url.agree_submit,
        async: true,
        data: {},
        dataType: 'json',
      });
      this.closeAgree();
    },
    closeAgree() {
      this.agree = true;
      try {
        sessionStorage.setItem('anta_agree_show_time', Date.now());
      } catch (error) {}
    },
  },
});
window.onload = function () {
  // /**商品模块**/
  // $('.loading').scrollLoading();
};
function areaInit() {
  pageResizeInit();
  /**大图轮播**/
  if ($('.model-swiper-container').length > 0) {
    var modelSwiper = new Swiper('.model-swiper-container', {
      loop: true,
      autoplay: {
        delay: 2500,
        disableOnInteraction: false,
      },
      pagination: {
        el: '.swiper-pagination',
        clickable: true,
      },
    });
  }
  /**商品轮播**/
  if ($('.swiperGoods-list').length > 0) {
    var goodsSwiper = new Swiper('.swiperGoods-list', {
      slidesPerView: 4,
      spaceBetween: 20,
      pagination: {
        el: '.swiper-pagination',
        type: 'progressbar',
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
    });
  }
  $('.loading').scrollLoading();
}
function pageResizeInit() {
  /* 页面窗口resize时调用函数，各页面内重新写并覆盖
   ** ！！！ mall.common.js中的window.onresize函数已调用
   */
  /**手风琴模块**/
  $('.accordion-body').width(parseFloat($('body').width()) * 0.85);
}
